package com.itheima.core.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.itheima.core.po.User;
import com.itheima.core.service.UserService;

/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;

	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode, String password, Model model, HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(usercode, password);
		if (user != null) {
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			if (usercode.equals("admin") && password.equals("123456")) {
				// 跳转到主页面
				return "redirect:customer/list.action";
			}
				return "StudentFind";
		} else {
			model.addAttribute("msg", "账号或密码错误，请重新输入！");
			// 返回到登录页面
				return "login";
		}
	}

	/**
	 * 用户注册
	 */
	@RequestMapping(value = "/add.action", method = RequestMethod.POST)
	public String add(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		String user_name=request.getParameter("user_name");
		String user_password=request.getParameter("user_password");
		
		User users=new User(user_name, user_password);
		userService.add(users);
		
		session.setAttribute("USER_SESSION", users);
		return "StudentFind";
	}

	@RequestMapping(value = "/customer/StudentFind.action")
	public String toStudentFind() {
		return "StudentFind";
	}

	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
		return "customer";
	}

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "redirect:login.action";
	}

	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}
	
	/**
	 * 向留言箱页面跳转
	 */
	@RequestMapping(value = "/tolyx", method = RequestMethod.GET)
	public String tolyx() {
		return "lyx";
	}
}
